package com.axonactive.yolo.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.axonactive.yolo.domain.SubscribedFlight;
import com.axonactive.yolo.domain.SubscribedFlightUserId;
import com.axonactive.yolo.exception.SubcribedFlightNotFoundException;
import com.axonactive.yolo.exception.SubcribedFlightUserIdNotFoundException;
import com.axonactive.yolo.service.interfaces.FlightNotificationService;
import com.axonactive.yolo.service.interfaces.SubscribedFlightService;
import com.axonactive.yolo.service.interfaces.SubscribedFlightUserIdService;
import com.axonactive.yolo.util.session.SessionHelper;

@Controller
@RequestMapping(value = "/subscribe")
public class SubscribeController {
	private static final Logger LOGGER = Logger
			.getLogger(SubscribeController.class);
	@Autowired
	private SubscribedFlightService subcribedFlightService;

	@Autowired
	private FlightNotificationService flightNotificationService;
	
	@Autowired
	private SubscribedFlightUserIdService subscribedFlightUserIdService;

	// Show subscribed flight list
	@RequestMapping(value = { "/list" }, method = RequestMethod.GET)
	public ModelAndView subscribeList(HttpSession session) {
		ModelAndView mv = new ModelAndView("subscribe");

		if (SessionHelper.isLoggedIn(session)) {
			int userId = SessionHelper.getUserId(session);
			List<SubscribedFlight> subcribedFlights = subcribedFlightService
					.findByUserId(userId);

			mv.addObject("subscribedFlights", subcribedFlights);
		} else {
			SessionHelper.setGoBackSubscribe(session, true);
			mv = new ModelAndView("account/login");
		}

		return mv;
	}

	// Unsubscribe a flight
	@RequestMapping(value = "/unsubscribe", method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView unsubcribeFlight(@RequestBody String id,
			HttpSession session) throws SubcribedFlightNotFoundException,
			SubcribedFlightUserIdNotFoundException {
		JSONObject object = new JSONObject(id);
		String flightId = object.getString("flightId");

		if (SessionHelper.isLoggedIn(session)) {
			int userId = SessionHelper.getUserId(session);
			SubscribedFlight flight = subcribedFlightService.findByFlightId(flightId);

			//check
			List<SubscribedFlightUserId> list=subscribedFlightUserIdService.findByFlightId(flightId);
			if(list.size()==1){
				subcribedFlightService.delete(flight.getId());
			}

			//always
			SubscribedFlightUserId subUser = subscribedFlightUserIdService.findByFlightIdAndUserId(flightId, userId);
			if(subUser!=null){
				subscribedFlightUserIdService.delete(subUser.getId());
			}
		}
		return new ModelAndView("redirect:/subscribe/list.html");
	}
	
	@RequestMapping(value = { "/link-to-subscribe" }, method = RequestMethod.POST)
	public ModelAndView linkToSubscribeFromNotification(@RequestParam(value="flightId", required=false) String flightId, HttpSession session,RedirectAttributes redirectAttribute) {
		ModelAndView _mav = new ModelAndView("redirect:/subscribe/list.html");
		if (SessionHelper.isLoggedIn(session)) {
			redirectAttribute.addFlashAttribute("flightId",flightId);
			LOGGER.info("FlightId "+flightId);
		}else{
			SessionHelper.setGoBackSubscribe(session, true);
			_mav = new ModelAndView("account/login");
		}
		return _mav;
	}

}